home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1458.dms / var1458.adf / Menus / Example8.c < prev    next >
C/C++ Source or Header  |  1992-05-01  |  18KB  |  452 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Intuition               Amiga C Club       */
  7. /* Chapter: Menus                       Tulevagen 22       */
  8. /* File:    Example8.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-01                                       */
  11. /* Version: 1.10                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* Same as Example1 except that we this time will verify any menu      */
  21. /* operations. If the user tries to activate this program's menu we    */
  22. /* check if the position of the pointer, and if it is somewhere at the */
  23. /* top of the window (less than 10 lines down) the menu operation will */
  24. /* continue as normal, else we cancel the menu operation.              */
  25.  
  26.  
  27.  
  28. #include <intuition/intuition.h>
  29.  
  30.  
  31.  
  32. struct IntuitionBase *IntuitionBase;
  33.  
  34.  
  35.  
  36. /*************************************************************************/
  37. /*                         F O U R T H   I T E M                         */
  38. /*************************************************************************/
  39.  
  40. /* The text for the fourth item: */
  41. struct IntuiText my_fourth_text=
  42. {
  43.   2,            /* FrontPen, black. */
  44.   0,            /* BackPen, not used since JAM1. */
  45.   JAM1,         /* DrawMode, do not change the background. */
  46.   CHECKWIDTH,   /* LeftEdge, CHECKWIDTH amount of pixels out. */
  47.                 /* This will leave enough space for the check mark. */
  48.   1,            /* TopEdge, 1 line down. */
  49.   NULL,         /* TextAttr, default font. */
  50.   "Italic",     /* IText, the string. */
  51.   NULL          /* NextItem, no link to other IntuiText structures. */
  52. };
  53.  
  54. /* The MenuItem structure for the fourth item: */
  55. struct MenuItem my_fourth_item=
  56. {
  57.   NULL,            /* NextItem, this is the last item in the list. */
  58.   0,               /* LeftEdge, 0 pixels out. */
  59.   30,              /* TopEdge, 30 lines down. */
  60.   150,             /* Width, 150 pixels wide. */
  61.   10,              /* Height, 10 lines high. */
  62.   ITEMTEXT|        /* Flags, render this item with text. */
  63.   ITEMENABLED|     /*        this item will be enabled. */
  64.   CHECKIT|         /*        it is an attribute item. */
  65.   HIGHCOMP,        /*        complement the colours when highlihted. */
  66.   0x00000001,      /* MutualExclude, mutualexclude the first item only. */
  67.   (APTR) &my_fourth_text, /* ItemFill, pointer to the text. */
  68.   NULL,            /* SelectFill, nothing since we complement the col. */
  69.   0,               /* Command, no command-key sequence. */
  70.   NULL,            /* SubItem, no subitem list. */
  71.   MENUNULL,        /* NextSelect, no items selected. */
  72. };
  73.  
  74.  
  75.  
  76. /*************************************************************************/
  77. /*                          T H I R D   I T E M                          */
  78. /*************************************************************************/
  79.  
  80. /* The text for the third item: */
  81. struct IntuiText my_third_text=
  82. {
  83.   2,            /* FrontPen, black. */
  84.   0,            /* BackPen, not used since JAM1. */
  85.   JAM1,         /* DrawMode, do not change the background. */
  86.   CHECKWIDTH,   /* LeftEdge, CHECKWIDTH amount of pixels out. */
  87.                 /* This will leave enough space for the check mark. */
  88.   1,            /* TopEdge, 1 line down. */
  89.   NULL,         /* TextAttr, default font. */
  90.   "Underlined", /* IText, the string. */
  91.   NULL          /* NextItem, no link to other IntuiText structures. */
  92. };
  93.  
  94. /* The MenuItem structure for the third item: */
  95. struct MenuItem my_third_item=
  96. {
  97.   &my_fourth_item, /* NextItem, linked to the fourth item. */
  98.   0,               /* LeftEdge, 0 pixels out. */
  99.   20,              /* TopEdge, 20 lines down. */
  100.   150,             /* Width, 150 pixels wide. */
  101.   10,              /* Height, 10 lines high. */
  102.   ITEMTEXT|        /* Flags, render this item with text. */
  103.   ITEMENABLED|     /*        this item will be enabled. */
  104.   CHECKIT|         /*        it is an attribute item. */
  105.   HIGHCOMP,        /*        complement the colours when highlihted. */
  106.   0x00000001,      /* MutualExclude, mutualexclude the first item only. */
  107.   (APTR) &my_third_text, /* ItemFill, pointer to the text. */
  108.   NULL,            /* SelectFill, nothing since we complement the col. */
  109.   0,               /* Command, no command-key sequence. */
  110.   NULL,            /* SubItem, no subitem list. */
  111.   MENUNULL,        /* NextSelect, no items selected. */
  112. };
  113.  
  114.  
  115.  
  116. /*************************************************************************/
  117. /*                         S E C O N D   I T E M                         */
  118. /*************************************************************************/
  119.  
  120. /* The text for the second item: */
  121. struct IntuiText my_second_text=
  122. {
  123.   2,          /* FrontPen, black. */
  124.   0,          /* BackPen, not used since JAM1. */
  125.   JAM1,       /* DrawMode, do not change the background. */
  126.   CHECKWIDTH, /* LeftEdge, CHECKWIDTH amount of pixels out. */
  127.               /* This will leave enough space for the check mark. */
  128.   1,          /* TopEdge, 1 line down. */
  129.   NULL,       /* TextAttr, default font. */
  130.   "Bold",     /* IText, the string. */
  131.   NULL        /* NextItem, no link to other IntuiText structures. */
  132. };
  133.  
  134. /* The MenuItem structure for the second item: */
  135. struct MenuItem my_second_item=
  136. {
  137.   &my_third_item,  /* NextItem, linked to the third item. */
  138.   0,               /* LeftEdge, 0 pixels out. */
  139.   10,              /* TopEdge, 10 lines down. */
  140.   150,             /* Width, 150 pixels wide. */
  141.   10,              /* Height, 10 lines high. */
  142.   ITEMTEXT|        /* Flags, render this item with text. */
  143.   ITEMENABLED|     /*        this item will be enabled. */
  144.   CHECKIT|         /*        it is an attribute item. */
  145.   HIGHCOMP,        /*        complement the colours when highlihted. */
  146.   0x00000001,      /* MutualExclude, mutualexclude the first item only. */
  147.   (APTR) &my_second_text, /* ItemFill, pointer to the text. */
  148.   NULL,            /* SelectFill, nothing since we complement the col. */
  149.   0,               /* Command, no command-key sequence. */
  150.   NULL,            /* SubItem, no subitem list. */
  151.   MENUNULL,        /* NextSelect, no items selected. */
  152. };
  153.  
  154.  
  155.  
  156. /*************************************************************************/
  157. /*                          F I R S T   I T E M                          */
  158. /*************************************************************************/
  159.  
  160. /* The text for the first item: */
  161. struct IntuiText my_first_text=
  162. {
  163.   2,          /* FrontPen, black. */
  164.   0,          /* BackPen, not used since JAM1. */
  165.   JAM1,       /* DrawMode, do not change the background. */
  166.   CHECKWIDTH, /* LeftEdge, CHECKWIDTH amount of pixels out. */
  167.               /* This will leave enough space for the check mark. */
  168.   1,          /* TopEdge, 1 line down. */
  169.   NULL,       /* TextAttr, default font. */
  170.   "Plain",    /* IText, the string. */
  171.   NULL        /* NextItem, no link to other IntuiText structures. */
  172. };
  173.  
  174. /* The MenuItem structure for the first item: */
  175. struct MenuItem my_first_item=
  176. {
  177.   &my_second_item, /* NextItem, linked to the second item. */
  178.   0,               /* LeftEdge, 0 pixels out. */
  179.   0,               /* TopEdge, 0 lines down. */
  180.   150,             /* Width, 150 pixels wide. */
  181.   10,              /* Height, 10 lines high. */
  182.   ITEMTEXT|        /* Flags, render this item with text. */
  183.   ITEMENABLED|     /*        this item will be enabled. */
  184.   CHECKIT|         /*        it is an attribute item. */
  185.   CHECKED|         /*        this item is initially selected. */
  186.   HIGHCOMP,        /*        complement the colours when highlihted. */
  187.   0xFFFFFFFE,      /* MutualExclude, mutualexclude all items except the */
  188.                    /*                first one. */
  189.   (APTR) &my_first_text, /* ItemFill, pointer to the text. */
  190.   NULL,            /* SelectFill, nothing since we complement the col. */
  191.   0,               /* Command, no command-key sequence. */
  192.   NULL,            /* SubItem, no subitem list. */
  193.   MENUNULL,        /* NextSelect, no items selected. */
  194. };
  195.  
  196.  
  197.  
  198. /*************************************************************************/
  199. /*                              M E N U                                  */
  200. /*************************************************************************/
  201.  
  202. /* The Menu structure for the first (and only) menu: */
  203. struct Menu my_menu=
  204. {
  205.   NULL,          /* NextMenu, no more menu structures. */
  206.   0,             /* LeftEdge, left corner. */
  207.   0,             /* TopEdge, for the moment ignored by Intuition. */
  208.   50,            /* Width, 50 pixels wide. */
  209.   0,             /* Height, for the moment ignored by Intuition. */
  210.   MENUENABLED,   /* Flags, this menu will be enabled. */
  211.   "Mode",        /* MenuName, the string. */
  212.   &my_first_item /* FirstItem, pointer to the first item in the list. */
  213. };
  214.  
  215.  
  216.  
  217. /* Declare a pointer to a Window structure: */ 
  218. struct Window *my_window;
  219.  
  220. /* Declare and initialize your NewWindow structure: */
  221. struct NewWindow my_new_window=
  222. {
  223.   0,             /* LeftEdge    x position of the window. */
  224.   0,             /* TopEdge     y positio of the window. */
  225.   320,           /* Width       320 pixels wide. */
  226.   100,           /* Height      100 lines high. */
  227.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  228.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  229.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  230.                  /*             user has selected the Close window gad. */
  231.   MENUPICK|      /*             Or if the user has done a menu operation, */
  232.   MENUVERIFY,    /*             or if the user tries to activate a menu. */
  233.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  234.   WINDOWCLOSE|   /*             Close Gadget. */
  235.   WINDOWDRAG|    /*             Drag gadget. */
  236.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  237.   WINDOWSIZING|  /*             Sizing Gadget. */
  238.   ACTIVATE,      /*             The window should be Active when opened. */
  239.   NULL,          /* FirstGadget No Custom gadgets. */
  240.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  241.   "Style Editor",/* Title       Title of the window. */
  242.   NULL,          /* Screen      Connected to the Workbench Screen. */
  243.   NULL,          /* BitMap      No Custom BitMap. */
  244.   80,            /* MinWidth    We will not allow the window to become */
  245.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  246.   300,           /* MaxWidth    than 300 x 200. */
  247.   200,           /* MaxHeight */
  248.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  249. };
  250.  
  251.  
  252.  
  253. main()
  254. {
  255.   /* Boolean variable used for the while loop: */
  256.   BOOL close_me;
  257.  
  258.   /* Declare a variable in which we will store the IDCMP flag: */
  259.   ULONG class;
  260.   
  261.   /* If we recieve a MENUPICK event, the Code field of the message */
  262.   /* structure will contain the menu number of the first selected item. */
  263.   /* Declare a variable to store the Code value in, and an extra menu */
  264.   /* number variable: */
  265.   USHORT code, menu_number;
  266.   
  267.   /* Declare a MenuItem pointer: */
  268.   struct MenuItem *item;
  269.   
  270.   /* Declare a pointer to an IntuiMessage structure: */
  271.   struct IntuiMessage *my_message;
  272.  
  273.  
  274.  
  275.   /* Before we can use Intuition we need to open the Intuition Library: */
  276.   IntuitionBase = (struct IntuitionBase *)
  277.     OpenLibrary( "intuition.library", 0 );
  278.   
  279.   if( IntuitionBase == NULL )
  280.     exit(); /* Could NOT open the Intuition Library! */
  281.  
  282.  
  283.  
  284.   /* We will now try to open the window: */
  285.   my_window = (struct Window *) OpenWindow( &my_new_window );
  286.   
  287.   /* Have we opened the window succesfully? */
  288.   if(my_window == NULL)
  289.   {
  290.     /* Could NOT open the Window! */
  291.     
  292.     /* Close the Intuition Library since we have opened it: */
  293.     CloseLibrary( IntuitionBase );
  294.  
  295.     exit();  
  296.   }
  297.  
  298.  
  299.  
  300.   /* We have opened the window, and everything seems to be OK. */
  301.  
  302.  
  303.  
  304.   SetMenuStrip( my_window, &my_menu );
  305.   printf("Menustrip connected to window!\n");
  306.  
  307.  
  308.   close_me = FALSE;
  309.  
  310.   /* Stay in the while loop until the user has selected the Close window */
  311.   /* gadget: */
  312.   while( close_me == FALSE )
  313.   {
  314.     /* Wait until we have recieved a message: */
  315.     Wait( 1 << my_window->UserPort->mp_SigBit );
  316.  
  317.     /* As long as we collect messages sucessfully we stay in the loop: */
  318.     while(my_message=(struct IntuiMessage *) GetMsg( my_window->UserPort ))
  319.     {
  320.       /* After we have collected the message we can read it, and save any */
  321.       /* important values which we maybe want to check later: */
  322.       class = my_message->Class;
  323.       code = my_message->Code;
  324.  
  325.  
  326.  
  327.       /* Before we reply we need to see if we have recieved a MENUVERIFY */
  328.       /* message: */
  329.       
  330.       if( class == MENUVERIFY )
  331.       {
  332.         /* Yes, we have recieved a MENUVERIFY message!                    */
  333.         /* The user wants to activate a menu, but the problem is that we  */
  334.         /* do not know if it is our window's menu that will be activated, */
  335.         /* or some other window's menu. We can however check it by        */
  336.         /* examining the Code field of the message. If it is equal to     */
  337.         /* MENUWAITING, it means that it is not your window's menu that   */
  338.         /* will be activated, but if Code is equal to MENUHOT it means it */
  339.         /* is is your window's menu that will be activated!               */
  340.  
  341.         if( code == MENUWAITING )
  342.         {
  343.           /* It is not your window's menu that will be activated! */
  344.           
  345.           /* Your program can take a pause if necessary. You maybe want */
  346.           /* to finish of with some drawings, so your program does not  */
  347.           /* trash any menus. This is especially important if you are   */
  348.           /* using the low-level graphics rutines since they do not     */
  349.           /* bother about, windows etc, and will draw over and destroy  */
  350.           /* anything in their way.                                     */
  351.           
  352.           /* Once the program is ready it should reply the message, and */
  353.           /* the menu will be activated.                                */
  354.         
  355.           printf("Another program's menu will be displayed!\n");
  356.         }
  357.         else
  358.           if( code == MENUHOT )
  359.           {
  360.             /* It is your window's menu that will be activated! */
  361.             
  362.             /* You can now take a pause and finish of with something     */
  363.             /* before you let Intuition activate the menu, or you can    */
  364.             /* even stop the whole menu operation if necessary. If you   */
  365.             /* are writing a paint program you maybe only want the user  */
  366.             /* to be able to activate the menu if the pointer is at the  */
  367.             /* top of the display. That would mean that the user can     */
  368.             /* draw with the right mouse button, and when the user wants */
  369.             /* to make a menu choice, he/she simply moves the pointer to */
  370.             /* the top of the display, and then presses the right mouse  */
  371.             /* button.                                                   */
  372.             
  373.             /* We will now check if the pointer is somewhere at the top  */
  374.             /* of the display: */
  375.             if( my_window->MouseY < 10)
  376.             {
  377.               /* The Y coordinate of the pointer is at least less than   */
  378.               /* 10 lines below the TopEdge of the window.               */
  379.             
  380.               /* The menu operation should continue as soon as possible! */
  381.               printf("OK!\n");
  382.             }
  383.             else
  384.             {
  385.               /* The pointer is below the Title bar of the window! */
  386.               /* Cancel the whole menu operation! */
  387.               
  388.               /* To cancel a menu operation you need to change the Code   */
  389.               /* field to MENUCANCEL. IMPORTANT! Do not change the code   */
  390.               /* variable since it is just a copy of the real Code value. */
  391.               /* What we need to do is to change the real value, and that */
  392.               /* is still OK since we have not replied yet.               */
  393.     
  394.               my_message->Code=MENUCANCEL;
  395.  
  396.               printf("Menu operation canceled!\n");
  397.             }
  398.           }
  399.       }
  400.  
  401.       /* After we have read it we reply as fast as possible: */
  402.       /* REMEMBER! Do never try to read a message after you have replied! */
  403.       /* Some other process has maybe changed it. */
  404.       ReplyMsg( my_message );
  405.  
  406.       /* Check which IDCMP flag was sent: */
  407.       if( class == CLOSEWINDOW )
  408.         close_me=TRUE; /* The user selected the Close window gadget! */  
  409.  
  410.       if(class == MENUPICK)
  411.       {
  412.         printf("\nMenu pick!\n");
  413.         menu_number = code;
  414.         
  415.         while( menu_number != MENUNULL )
  416.         {
  417.           /* Get the address of the item: */
  418.           item = (struct MenuItem *) ItemAddress( &my_menu, menu_number );
  419.  
  420.  
  421.           /* Print out the menu number plus etc: */
  422.           printf("menu_number= %d\n", menu_number );
  423.           printf("MENUNUM = %d\n", MENUNUM(menu_number) );
  424.           printf("ITEMNUM = %d\n", ITEMNUM(menu_number) );
  425.           printf("SUBNUM  = %d\n", SUBNUM(menu_number) );
  426.  
  427.  
  428.           /* Get the following item's menu number: */
  429.           menu_number = item->NextSelect;
  430.         }
  431.       }
  432.     }
  433.   }
  434.  
  435.  
  436.  
  437.   printf("Menustrip removed from window!\n");
  438.   ClearMenuStrip( my_window );
  439.  
  440.  
  441.  
  442.   /* Close the window: */
  443.   CloseWindow( my_window );
  444.  
  445.  
  446.  
  447.   /* Close the Intuition Library since we have opened it: */
  448.   CloseLibrary( IntuitionBase );
  449.   
  450.   /* THE END */
  451. }
  452.